package org.neogenesis.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.log4j.Logger;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.neogenesis.web.model.Platform;
import org.neogenesis.web.model.Sample;

public class Utils {

	public static String untarFolder(String filePath, String fileName)
			throws IOException {

		String os = System.getProperty("os.name");

		if (os.startsWith("Windows"))
			return untarFolderWindow(filePath, fileName);

		return untarFolderLinux(filePath, fileName);
	}
	
	public static String unzipFolder(String filePath, String fileName)
			throws IOException {

		String os = System.getProperty("os.name");

		if (os.startsWith("Windows"))
			return unzipFolderWindow(filePath, fileName);
		
		//unzipFolderLinux(filePath, fileName);
		return null;
	}

	private static String untarFolderLinux(String filePath, String fileName)
			throws IOException {

		String tarFile = filePath + fileName;

		String folderPath = filePath + getArchiveName(fileName) + "/";

		File folder = new File(folderPath);

		if (folder.exists())
			folder.delete();

		folder.mkdir();

		String command = "tar -xvf " + tarFile + " -C " + folderPath;

		Runtime.getRuntime().exec(command);

		return folderPath;
	}

	private static String untarFolderWindow(String filePath, String fileName)
			throws IOException {

		String tarFile = filePath + fileName;

		String folderPath = filePath + getArchiveName(fileName) + "/";

		File folder = new File(folderPath);

		if (folder.exists())
			folder.delete();

		folder.mkdir();

		TarInputStream tin = new TarInputStream(new GZIPInputStream(
				new FileInputStream(tarFile)));
		TarEntry tarEntry = tin.getNextEntry();
		while (tarEntry != null) {
			File destPath = new File(folderPath + File.separatorChar
					+ tarEntry.getName());
			if (tarEntry.isDirectory()) {
				destPath.mkdir();
			} else {
				FileOutputStream fout = new FileOutputStream(destPath);
				tin.copyEntryContents(fout);
				fout.close();
			}
			tarEntry = tin.getNextEntry();
		}
		tin.close();
		tin = null;

		return folderPath;
	}

	private static String unzipFolderWindow(String filePath, String fileName)
			throws IOException {

		String zipFileName = filePath + fileName;

		String folderPath = filePath + getArchiveName(fileName) + "/";

		File file = new File(zipFileName);
		File folder = new File(folderPath);

		if (folder.exists())
			folder.delete();

		folder.mkdir();

		try {

			ZipEntry entry;
			BufferedOutputStream dest = null;
			BufferedInputStream is = null;
			ZipFile zipFile = new ZipFile(file);
			Enumeration e = zipFile.entries();

			while (e.hasMoreElements()) {
				entry = (ZipEntry) e.nextElement();

				is = new BufferedInputStream(zipFile.getInputStream(entry));
				int count;

				byte data[] = new byte[1024];
				FileOutputStream fos = new FileOutputStream(folder.toString() +"/"+ entry.getName());
				dest = new BufferedOutputStream(fos, 1024);

				while ((count = is.read(data, 0, 1024)) != -1) {
					dest.write(data, 0, count);
				}
				dest.flush();
				dest.close();
				is.close();
			}
			zipFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return folderPath;
	}

	public static boolean deleteFile(String filePath) {
		File file = new File(filePath);
		return file.delete();
	}

	public static String getArchiveName(String fileName) {

		int pos = fileName.lastIndexOf(".");

		return fileName.substring(0, pos);
	}

	public static String compress(StringBuffer buffer) throws IOException {

		return null;
	}

	public static String getUniqueNameSample(Sample sample) {

		String name = sample.getExperiment().getSeriesIid()
				+ Configurations.SEPARATOR + sample.getName()
				+ Configurations.FILE_EXTENSION;

		return name;
	}

	public static String getUniqueNamePlatform(Platform platform) {
		String name = platform.getAccessionName();
		return name;
	}

	public static void deleteFolder(String folderGSEPath) {
		String os = System.getProperty("os.name");

		if (os.startsWith("Windows")) {
			deleteFolderWindows(folderGSEPath);
		} else
			deleteFolderLinux(folderGSEPath);
	}

	private static void deleteFolderLinux(String folderGSEPath) {

		String command = "rm -r " + folderGSEPath;

		try {
			Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private static boolean deleteFolderWindows(String filePath) {

		Logger.getLogger("Removing Folder Content:");
		
		File file = new File(filePath);
		if (file.isDirectory()) {
			String[] children = file.list();
			String folderPath = file.getAbsolutePath();
			for (String childPath : children) {
				if (folderPath.charAt(folderPath.length() - 1) != '/')
					folderPath = folderPath + "/";
					Logger.getLogger("Removed: " + deleteFolderWindows(folderPath + childPath));
			}
		}
		// The directory is now empty so delete it
		return file.delete();

	}

	public static void main(String[] args) {

		System.out.println(deleteFolderWindows("target/aaa/"));
	}
}
